package com.a01_2021.thousandstimes.general.controller;


import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.general.entity.School;
import com.a01_2021.thousandstimes.general.service.ISchoolService;
import com.a01_2021.thousandstimes.utils.AjaxResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author E1xP
 * @since 2021-04-03
 */
@RestController
@RequestMapping("/school")
public class SchoolController {

    @Resource
    ISchoolService schoolService;

    /**
     * 用于添加在校记录
     *
     * @param instanceId 实例id
     * @param school     在校记录实体类
     * @return AjaxResponse
     * @throws CustomException 当存在同生日、名字记录时抛出该异常
     */
    @PreAuthorize("hasAuthority('school-add')")
    @DS("master")
    @PostMapping("add")
    public AjaxResponse add(@RequestHeader("instanceid") String instanceId, School school) throws CustomException {
        if (!instanceId.equals("master")) {
            return AjaxResponse.error("该接口不允许实例调用");
        }
        School result = schoolService.add(school);
        return AjaxResponse.success(result);
    }

}
